package com.Artificialintelligence.web.controller.system;

import com.Artificialintelligence.common.constant.Constants;
import com.Artificialintelligence.common.core.domain.AjaxResult;
import com.Artificialintelligence.common.core.domain.entity.SysMenu;
import com.Artificialintelligence.common.core.domain.entity.SysUser;
import com.Artificialintelligence.common.core.domain.model.Giteelogin;
import com.Artificialintelligence.common.core.domain.model.LoginBody;
import com.Artificialintelligence.common.utils.SecurityUtils;
import com.Artificialintelligence.system.domain.SysAuthUser;
import com.Artificialintelligence.system.service.*;
import me.zhyd.oauth.model.AuthCallback;
import me.zhyd.oauth.request.AuthRequest;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 登录验证
 *
 * @author ruoyi
 */
@RestController
public class SysLoginController
{
    @Autowired
    private SysLoginService loginService;

    @Autowired
    private ISysMenuService menuService;

    @Autowired
    private ISysUserService iSysUserService;

    @Autowired
    private SysPermissionService permissionService;

    @Autowired
    private ISysAuthUserService sysAuthUserService;

    /**
     * 登录方法
     *
     * @param loginBody 登录信息
     * @return 结果
     */
    @PostMapping("/login")
    public AjaxResult login(@RequestBody LoginBody loginBody)
    {
		Map<String,Object> ajax = new HashMap<>();
        String token = null;
        // 校验是否是第三方登陆
        // 生成令牌--正常登陆
        if(loginBody.getCode()!=null && loginBody.getCode().length()>0) {
            token = loginService.login(loginBody.getUsername(), loginBody.getPassword(), loginBody.getCode(), loginBody.getUuid());
        }
//        // 生成令牌--第三方登陆
//        if(loginBody.getCode()==null || loginBody.getCode().length()==0){
//            loginBody.setSource(loginBody.getPassword());
//            SysAuthUser sysAuthUser = sysAuthUserService.selelerauthusre(loginBody);
//            if(sysAuthUser != null){
//                token = loginService .authlogin(loginBody,sysAuthUser);
//            }else {
//                return AjaxResult.error("系统错误，无法登陆，请联系管理员");
//            }
//        }

        ajax.put(Constants.TOKEN, token);
        return AjaxResult.success(ajax);
    }

    /**
     * 第三方登录验证用户--gitee
     *
     * @param giteelogin 用户信息
     * @return 结果
     */
    @PostMapping("/giteelogin")
    public AjaxResult giteelogin(@RequestBody Giteelogin giteelogin)
    {
        Map<String,Object> ajax = new HashMap<>();
        if(giteelogin.getUuid()!=null) {
            SysUser sysUser = iSysUserService.giteeusre(giteelogin);
            if (sysUser!=null) {
                ajax.put("code",200);
                ajax.put("user", giteelogin);
                ajax.put("sysUser",sysUser);
                return AjaxResult.success("200",ajax);
            } else {
                ajax.put("code",500);
                ajax.put("codename","没有找到相应的账号，请绑定账号后重新登陆");
                ajax.put("user", giteelogin);
                return AjaxResult.success("500",ajax);
            }
        }else {
            ajax.put("user", "gitee没有该用户的id");
            return AjaxResult.error("gitee没有该用户",ajax);
        }
    }

    /**
     * 获取用户信息
     *
     * @return 用户信息
     */
    @GetMapping("getInfo")
    public AjaxResult getInfo()
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        // 角色集合
        Set<String> roles = permissionService.getRolePermission(user);
        // 权限集合
        Set<String> permissions = permissionService.getMenuPermission(user);
		Map<String,Object> ajax = new HashMap<>();
        ajax.put("user", user);
        ajax.put("roles", roles);
        ajax.put("permissions", permissions);
        return AjaxResult.success(ajax);
    }

    /**
     * 获取路由信息
     *
     * @return 路由信息
     */
    @GetMapping("getRouters")
    public AjaxResult getRouters()
    {
        Long userId = SecurityUtils.getUserId();
        List<SysMenu> menus = menuService.selectMenuTreeByUserId(userId);
        return AjaxResult.success(menuService.buildMenus(menus));
    }
}
